package com.sykj.springboot.syslog;
import java.util.Enumeration;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.sykj.springboot.common.contants.MapCatchs;
import com.sykj.springboot.common.entity.JsonResult;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


@Aspect
@Component
public class LogAspect {

	private Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${filter}")
    private String filter;

	//凡是在方法上加入了@CustomLog这个注解，那么就会执行日志记录
	@Pointcut("@annotation(com.sykj.springboot.syslog.CustomLog)")
	public void systemLog(){

	}

	@Before("systemLog()")
	public void doBefore(JoinPoint joinPoint){
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        logger.info("---------------->URL : " + request.getRequestURL().toString());
        logger.info("---------------->HTTP_METHOD : " + request.getMethod());
        logger.info("---------------->IP : " + request.getRemoteAddr());
        Enumeration<String> enu = request.getParameterNames();
        while (enu.hasMoreElements()) {
            String name = (String) enu.nextElement();
            logger.info("params---->"+ name +":"+ request.getParameter(name)+"\n");
        }

        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        CustomLog annotation = methodSignature.getMethod().getAnnotation(CustomLog.class);
        String action = annotation.action();
        logger.info("action:---->"+action);
        String description = annotation.description();
        logger.info("description:---->"+description);

        // 这里可以记录日志到数据库。。。。。

    }

    @Around("systemLog()")
    public Object doArund(ProceedingJoinPoint pjp)  throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String string = request.getRequestURL().toString();
        String[] split = filter.split(",");
        boolean flag= false;
        for (String str:split) {
            if (string.contains(str)){
                flag = true;
            }
            if (!flag){
                String token = request.getParameter("token");
                if (!StringUtils.isEmpty(token)){
                    String[] split1 = token.split(";");
                    for (String ste:split1) {
                        if (ste.contains("vat_login_token")){
                            String vat_login_token = ste.split("=")[1];
                            if (MapCatchs.USER_MAP.get(vat_login_token)==null){
                                return  new JsonResult("用户未登录",JsonResult.TIME_OUT);
                            }
                        }
                    }
                }else{
                    return  new JsonResult("用户未登录",JsonResult.TIME_OUT);
                }

            }

        }
        Object ob = pjp.proceed();
        return ob;
    }

	@AfterReturning(returning = "ret", pointcut = "systemLog()")
	public void doAfterReturning(Object ret) throws Throwable {
		logger.info("------------------------------->response :" + ret);
	}

}
